package model;


import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import Entity.*;
import S2.server.ConnectionManage;


public class UsersModel {

	private static Connection conn = ConnectionManage.getInstance()
			.getConnection();


	public static User getUser(String name, String pass) throws SQLException {

		String sql = "SELECT * FROM" + " users WHERE" + " name=? AND password=?";
		ResultSet rs = null;
		User bean =  new User();

		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			stmt.setString(1, name);
			stmt.setString(2, pass);
			rs = stmt.executeQuery();
			
			if (rs.next()) {
				bean.setName(name);
				bean.setPassword(pass);
				bean.setRole(rs.getInt("role"));
				return bean;
			} else {
				return null;
			}

		} catch (SQLException e) {
			System.err.println(e);
			return null;
		} finally {
			if (rs != null) {
				rs.close();
			}
		}

	}
	/*public static User addUser(String Id,String name, String pass, int role) throws SQLException {
		String sql = "INSERT INTO USER (id,name,age,rate)"+"VALUES(?,?,?,?)";
		ResultSet rs = null;
		User bean =  new User();
		
		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			stmt.setString(1, Id);
			stmt.setString(2, name);
			stmt.setString(3, pass);
			stmt.setInt(4 , role);
			
		
			
			rs = stmt.executeQuery();
			if (rs.next()) {
				bean.setName(name);
				bean.setId(Id);
				bean.setPassword(pass);
				bean.setRole(role);
				return bean;
				
			}
			else {
					return null;
			} 
		
			}
			catch (SQLException e) {
				System.err.println(e);
				return null;
			} finally {
				if (rs != null) {
					rs.close();
				}
			}
	}*/
	
	
	
	/*public static void deleteUser(String Id,String name) throws SQLException {

		String sql = "DELETE FROM user " +  "WHERE name = ?" + "AND Id =?";
		ResultSet rs = null;
		

		try (PreparedStatement stmt = conn.prepareStatement(sql);)
		{		
			stmt.setString(1, name);
			stmt.setString(2,Id);
			rs = stmt.executeQuery();
		}
		 catch (SQLException e) {
				System.err.println(e);
				
			} finally {
				if (rs != null) {
					rs.close();
				}
			
	}
}*/
	
	
	
/*	public static User updateUser(String Id,String name, String pass, int role) throws SQLException {
		String sql = "update INTO USER (id,name,age,rate)"+"VALUES(?,?,?,?)";
		ResultSet rs = null;
		User bean =  new User();
		
		try (PreparedStatement stmt = conn.prepareStatement(sql);) {
			stmt.setString(1, Id);
			stmt.setString(2, name);
			stmt.setString(3, pass);
			stmt.setInt(4 , role);
			
		
			
			rs = stmt.executeQuery();
			if (rs.next()) {
				bean.setName(name);
				bean.setId(Id);
				bean.setPassword(pass);
				bean.setRole(role);
				return bean;
				
			}
			else {
					return null;
			} 
		
			}
			catch (SQLException e) {
				System.err.println(e);
				return null;
			} finally {
				if (rs != null) {
					rs.close();
				}
			}
		
	}*/
}